package com.example.servlet.user;

import com.example.entity.Passenger;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;

@WebServlet("/user_member_center")
public class uMemberCenterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try (SqlSession Session = MybatisUtil.getSession()){
            UserService service = new UserServiceImpl(Session);
            User user = (User) req.getSession().getAttribute("user");
            Passenger passenger = service.getPassengerByIDtypeAndIDcard(user.getID_type(), user.getID_card());
            double mileage = passenger.getMileage();
            Context context = new Context();
            context.setVariable("user", user);
            context.setVariable("mileage", mileage);
            // 图片是否存在判断
            String imagePath = getServletContext().getRealPath("/static/images/" + user.getU_id() + ".jpg");
            File imageFile = new File(imagePath);
            boolean imageExists = imageFile.exists();
            context.setVariable("imageExists", imageExists);
            ThymeleafUtil.process("user_member_center.html", context, resp.getWriter());
        }
    }
}
